package geppetto.cat.programs;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentEvaluator;
import geppetto.cat.alignments.AlignmentStats;
import geppetto.cat.alignments.AlignmentSymetrization;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.AlignmentEvaluator.Evaluation;
import geppetto.cat.alignments.output.AlignerOutputLatex;
import geppetto.cat.corpus.BilingualCorpus;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.zip.GZIPInputStream;



/**
 * This class gets a file with the output of GizaM4 results
 * loads them and evaluates and prints them.
 * @author javg
 *
 */
public class LoadAndEvalGizaM4Results {

	public static void main(String[] args) throws IOException {
		System.out.println("Start main");
		String corpusDescription = args[0];
		int size = Integer.parseInt(args[1]); // Needs to be at most as much as the gold cor
		int setSize = Integer.parseInt(args[2]);
		String gizaDir = args[3];
		String outputDir = args[4];
		System.out.println("Size " + size);
		System.out.println("setSize " + setSize);
		System.out.println("GizaDir " + gizaDir);
		System.out.println("OutputDir " + outputDir);
	

		
		BilingualCorpus forwardCorpus = BilingualCorpus.getCorpusFromFileDescription(
				corpusDescription, size);
		BilingualCorpus backwardCorpus = forwardCorpus.reverse(); 
		String sSuf = forwardCorpus.getSourceSufix();
		String fSuf = forwardCorpus.getForeignSufix();
		String forwardFile = gizaDir + "giza."+ fSuf+"-"+sSuf + "/" + fSuf+"-"+sSuf +".A3.final.gz";	
		BufferedReader gizaForward = new BufferedReader(
				new InputStreamReader(new GZIPInputStream(new FileInputStream(forwardFile)),"UTF8"));
		String backwardFile = gizaDir + "giza."+ sSuf+"-"+fSuf + "/" + sSuf+"-"+fSuf +".A3.final.gz";
		BufferedReader gizaBackward = new BufferedReader(
				new InputStreamReader(new GZIPInputStream(new FileInputStream(backwardFile)),"UTF8"));
		
		AlignmentsSet m4Forward = new AlignmentsSet();
		AlignmentsSet m4Backward = new AlignmentsSet();
		for(int i = 0; i < setSize; i++){
			String fline1 = gizaForward.readLine();
			String fline2 = gizaForward.readLine();
			String fline3 = gizaForward.readLine();
			String bline1 = gizaBackward.readLine();
			String bline2 = gizaBackward.readLine();
			String bline3 = gizaBackward.readLine();
			m4Forward.addAlignment(Alignment.fromGizaFormat(forwardCorpus, i, BilingualCorpus.TEST_CORPUS, fline1, fline2, fline3));
			m4Backward.addAlignment(Alignment.fromGizaFormat(backwardCorpus, i, BilingualCorpus.TEST_CORPUS, bline1, bline2, bline3));
		}
		
		
		
		Evaluation forwardEval = AlignmentEvaluator.evaluate(m4Forward, forwardCorpus.getGold());
		System.out.println("Forward eval " + forwardEval);
		Evaluation[] forwardEvalRare = AlignmentEvaluator.evaluateRareWords(m4Forward, forwardCorpus.getGold(),forwardCorpus);
		System.out.println("Forward eval rare words" + forwardEvalRare[0]);
		System.out.println("Forward eval non-rare words" + forwardEvalRare[1]);
		AlignmentStats.printPhrases(m4Forward, true, false);
		
		
		Evaluation backwardEval = AlignmentEvaluator.evaluate(m4Backward, backwardCorpus.getGold());
		System.out.println("Backward eval " + backwardEval);
		Evaluation[] backwardEvalRare = AlignmentEvaluator.evaluateRareWords(m4Backward, backwardCorpus.getGold(),backwardCorpus);
		System.out.println("Bacward eval rare words" + backwardEvalRare[0]);
		System.out.println("Backwar eval non-rare words" + backwardEvalRare[1]);
		AlignmentStats.printPhrases(m4Backward, true, false);
		
		AlignmentsSet m4Intersection = AlignmentSymetrization.intersect(m4Forward, m4Backward);
		Evaluation intersectionEval = AlignmentEvaluator.evaluate(m4Intersection, forwardCorpus.getGold());
		System.out.println("Intersection eval " + intersectionEval);
		Evaluation[] intersectionEvalRare = AlignmentEvaluator.evaluateRareWords(m4Intersection, forwardCorpus.getGold(),forwardCorpus);
		System.out.println("Intersection eval rare words" + intersectionEvalRare[0]);
		System.out.println("Intersection eval non-rare words" + intersectionEvalRare[1]);
		AlignmentStats.printPhrases(m4Intersection, true, false);
		
		
		AlignmentsSet m4GrowDiagFinal = AlignmentSymetrization.growDiagFinal(m4Forward, m4Backward);
		Evaluation gdfEval = AlignmentEvaluator.evaluate(m4GrowDiagFinal, forwardCorpus.getGold());
		System.out.println("GDF eval " + gdfEval);
		
		Evaluation[] gdfEvalRare = AlignmentEvaluator.evaluateRareWords(m4GrowDiagFinal, forwardCorpus.getGold(),forwardCorpus);
		System.out.println("GDF eval rare words" + gdfEvalRare[0]);
		System.out.println("GDF eval non-rare words" + gdfEvalRare[1]);
		AlignmentStats.printPhrases(m4GrowDiagFinal, true, false);
		
		System.out.println("Print same information but in table format");
		
		System.out.println(forwardEval.toSimpleString());
		
		System.out.println(forwardEvalRare[0].toSimpleString());
		System.out.println(forwardEvalRare[1].toSimpleString());
		
		System.out.println(backwardEval.toSimpleString());
		
		System.out.println(backwardEvalRare[0].toSimpleString());
		System.out.println(backwardEvalRare[1].toSimpleString());
		
		
		System.out.println(intersectionEval.toSimpleString());
		System.out.println(intersectionEvalRare[0].toSimpleString());
		System.out.println(intersectionEvalRare[1].toSimpleString());
		
		System.out.println(gdfEval.toSimpleString());
		System.out.println(gdfEvalRare[0].toSimpleString());
		System.out.println(gdfEvalRare[1].toSimpleString());
		
		System.out.println("Ended Print same information but in table format");
		
		
		PrintStream forwardPrinter = new PrintStream(outputDir
				+ "/forward.tex");
		AlignerOutputLatex.printBegin(forwardPrinter);
		AlignerOutputLatex.outputWithGoldPostLatex(m4Forward, forwardCorpus.getGold(), forwardCorpus, forwardPrinter, 15, "");
		AlignerOutputLatex.printEnd(forwardPrinter);
		
		PrintStream backwardPrinter = new PrintStream(outputDir
				+ "/backward.tex");
		AlignerOutputLatex.printBegin(backwardPrinter);
		AlignerOutputLatex.outputWithGoldPostLatex(m4Backward, backwardCorpus.getGold(), backwardCorpus, backwardPrinter, 15, "");
		AlignerOutputLatex.printEnd(backwardPrinter);
		
		PrintStream intersectionPrinter = new PrintStream(outputDir
				+ "/intersect.tex"); 
		AlignerOutputLatex.printBegin(intersectionPrinter);
		AlignerOutputLatex.outputWithGoldPostLatex(m4Intersection, forwardCorpus.getGold(), forwardCorpus, intersectionPrinter, 15, "");
		AlignerOutputLatex.printEnd(intersectionPrinter);
		
		PrintStream gdfPrinter = new PrintStream(outputDir
				+ "/gdf.tex"); 
		AlignerOutputLatex.printBegin(gdfPrinter);
		AlignerOutputLatex.outputWithGoldPostLatex(m4GrowDiagFinal, forwardCorpus.getGold(), forwardCorpus, gdfPrinter, 15, "");
		AlignerOutputLatex.printEnd(gdfPrinter);
	
	}
	
}
